package com.ymatou.iapi.dglogin.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dglogin.parameter.AppSourceEnum;
import com.ymatou.iapi.dglogin.parameter.WebWxLoginBean;
import com.ymatou.iapi.dglogin.service.WebWxLoginCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
/**
*web微信扫码登录
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_WebWxLogin{
	private static WebWxLoginBean webwxloginBean;
	private static WebWxLoginCall webwxloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("web微信扫码登录");
	}

	@Before
	public void caseUp() {
		webwxloginBean=new WebWxLoginBean();
		webwxloginCall=new WebWxLoginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(webwxloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_001() {
		Logger.start(true,"web微信扫码登录,成功-默认商家");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("true", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals(userid.toString(), webwxloginCall.getString("userId"), "验证userId");
			Logger.verifyNotNull(webwxloginCall.getString("accessToken"), "验证accessToken");
			Map<String, Object> token1 = new UserTokenWapper().getUserTokenByUid(userid);
			Logger.comment(token1.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token1.get("tSource"), "验证tSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_002() {
		Logger.start(true,"web微信扫码登录,成功-默认客服");
		try {
			UserCreateBean u = DGUserCallService.initCsUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("true", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals(userid.toString(), webwxloginCall.getString("userId"), "验证userId");
			Logger.verifyNotNull(webwxloginCall.getString("accessToken"), "验证accessToken");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_WebWxLogin_003() {
		Logger.start(true,"web微信扫码登录,失败-默认用户");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			//errorMessage":"请求用户类型与当前小程序允许的用户类型不一致，wxAppName:wxsqBuyer, allowUserType:0, reqUserType:1"}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_004() {
		Logger.start(true,"web微信扫码登录,失败-独立用户");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setAppSource(DGUserCallService.DG_SellerWxApp);
			u.setWxAppName(DGUserCallService.TestWxsqOther);
			u.setBelongSellerId(DGUserCallService.TestWxsqOtherSellerId);
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_005() {
		Logger.start(true,"web微信扫码登录,openid不存,unionid存在成功");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId()+1);
			
			webwxloginCall.addHeader("mockId-2", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId()+1);
			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("true", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals(userid.toString(), webwxloginCall.getString("userId"), "验证userId");
			Logger.verifyNotNull(webwxloginCall.getString("accessToken"), "验证accessToken");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_006() {
		Logger.start(true,"web微信扫码登录,openid不存,unionid不存在登入失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId()+1);
			
			webwxloginCall.addHeader("mockId-2", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId()+1);
			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId()+1);
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("100", webwxloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_007() {
		Logger.start(true,"web微信扫码登录,openid和wxAppName不匹配失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("2", webwxloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_008() {
		Logger.start(true,"web微信扫码登录,不输入wxAppName失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
//			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", webwxloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_009() {
		Logger.start(true,"web微信扫码登录,不输入code失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
//			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", webwxloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_010() {
		Logger.start(true,"web微信扫码登录,不输入clientIp成功");
		//ip可以不输入
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
//			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("true", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals(userid.toString(), webwxloginCall.getString("userId"), "验证userId");
			Logger.verifyNotNull(webwxloginCall.getString("accessToken"), "验证accessToken");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_011() {
		Logger.start(true,"web微信扫码登录,不输入appsource失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
//			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", webwxloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_WebWxLogin_012() {
		Logger.start(true,"账号删除web微信扫码登录,失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer userid = DGUserCallService.UserCreateCall(u);
			DGUserCallService.DeleteUser(userid);
			Thread.sleep(5000);
			webwxloginBean.setClientIp(TestDataManager.getRandomIp());
			webwxloginBean.setCode("abcdefg");
			webwxloginBean.setAppSource(u.getAppSource());
			webwxloginBean.setWxAppName(DGUserCallService.TestWxsqSeller);
			webwxloginCall.setData(webwxloginBean);
			webwxloginCall.addHeader("mock", "mock-enable");
			webwxloginCall.addHeader("mockId-1", TestDataManager.getRandomUUID().toString());
			webwxloginCall.addHeader("mockApi-1", "dgLoginWxClient-getAccessToken-Normal");
			String token=("token" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			webwxloginCall.addHeader("mockParam-1-token",token);
			webwxloginCall.addHeader("mockParam-1-openId",u.getOpenId());
			
//			webwxloginCall.addHeader("mockApi-2", "dgLoginWxClient-getUserInfo-Normal");
//			webwxloginCall.addHeader("mockParam-2-openId",u.getOpenId());
//			webwxloginCall.addHeader("mockParam-2-unionId",u.getUnionId());
			webwxloginCall.callService();			
			Logger.verifyEquals("false", webwxloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}